export default {
  formatDate(_d) {
    //console.log(_d);
    var date;
    if (!_d)return "";
    else
      date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? '0' + m : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + m + '-' + d;
  },
  formatDateCN(_d) {
    //console.log(_d);
    var date;
    if (!_d)return "";
    else
      date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? m : m;
    var d = date.getDate();
    d = d < 10 ? (d) : d;
    if (y.toString() === 'NaN') return "";
    return y + '年' + m + '月' + d + "日";
  },
  formatDateYear(_d) {
    //console.log(_d);
    var date;
    if (!_d)return "";
    else
      date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? m : m;
    var d = date.getDate();
    d = d < 10 ? (d) : d;
    if (y.toString() === 'NaN') return "";
    return y;
  },
  formatDateMMddCN(_d) {
    //console.log(_d);
    var date;
    if (!_d)return "";
    else
      date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? m : m;
    var d = date.getDate();
    d = d < 10 ? (d) : d;
    if (y.toString() === 'NaN') return "";
    return m + '月' + d + "日";
  },
  formatMMdd(_d) {
    //console.log(_d);
    var date;
    if (!_d)return "";
    else
      date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? '0' + m : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    if (y.toString() === 'NaN') return "";
    return m + '-' + d;
  },
  formatDateYYMM(_d) {
    var date;
    if (_d == null)
      date = new Date();
    else
      date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? m : m;
    var d = date.getDate();
    d = d < 10 ? (d) : d;
    if (y.toString() === 'NaN') return "";
    return y + '年' + m + "月";
  },
  formatDateTime(_d) {
    if (!_d) return "";
    var date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
  },
  formatDateTimeCN(_d) {
    var date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? (m) : m;
    var d = date.getDate();
    d = d < 10 ? (d) : d;
    var h = date.getHours();
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    if (y.toString() === 'NaN') return "";
    return y + '年' + m + '月' + d + '日 ' + h + ':' + minute;
  },

  formatTime(_d) {
    var date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    return h + ':' + minute;
  },
  addDay(_d, day) {
    var date = this.getDate(_d);
    date.setDate(date.getDate() + day);
    return date;
  },
  addMonth(_d, m) {
    var date = this.getDate(_d);
    date.setMonth(date.getMonth() + m);
    return date;
  },
  getWeek(_d, week) {
    //week是星期 1-7
    var date = this.getDate(_d);
    var _w = date.getDay();
    if (_w == 0) _w = 7;
    if (week > _w) date.setDate(date.getDate() + (week - _w));
    if (week < _w) date.setDate(date.getDate() - (_w - week));
    return date;
  },
  getMonthFirstDay(_d) {
    //本月第一天
    var date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var date_now = new Date(y, m - 1, 1);
    return date_now;
  },
  getMonthLastDay(_d) {
    //本月最后一天
    var date = this.getDate(_d);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var date_now = new Date(y, m, 0);
    return date_now;
  },
  getDate(_d) {
    _d = _d + "";

    if (this.checkDateTime(_d)) {
      return new Date(_d.replace(/-/g, "/"));
    } else if (this.checkDate(_d)) {
      return new Date(_d.replace(/-/g, "/"));
    }

    if (_d.length == 13)
      return new Date(_d * 1);
    else if (_d.length == 10)
      return new Date(_d * 1000);
    return new Date(_d);
  },
  checkDateTime(str) {
    var reg = /^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
    var r = str.match(reg);
    if (r == null) return false;
    r[2] = r[2] - 1;
    var d = new Date(r[1], r[2], r[3], r[4], r[5], r[6]);
    if (d.getFullYear() != r[1]) return false;
    if (d.getMonth() != r[2]) return false;
    if (d.getDate() != r[3]) return false;
    if (d.getHours() != r[4]) return false;
    if (d.getMinutes() != r[5]) return false;
    if (d.getSeconds() != r[6]) return false;
    return true;
  },
  checkDate(mystring) {
    var reg = /^(\d{4})-(\d{2})-(\d{2})$/;
    var str = mystring;
    var arr = reg.exec(str);
    if (arr == null) return false;
    if (!reg.test(str) && RegExp.$2 <= 12 && RegExp.$3 <= 31) {
      return false;
    }
    return true;
  },
  get_date_day(day) {
    if (day == null) day = new Date().getTime();
    return this.getDate(this.formatDate(day)).getTime();
  },


  ele_picker_options(min, max, config) {
    return {
      disabledDate: (time) => {
        return this._ele_picker_options_disabled_date(time, min, max);
      },
      ...config
    }
  },
  ele_picker_options_min(min, config) {
    if (min == null) min = new Date().getTime();
    return {
      disabledDate: (time) => {
        return this._ele_picker_options_disabled_date_min(time, min);
      },
      ...config
    }
  },
  ele_picker_options_max(max, config) {
    return {
      disabledDate: (time) => {
        return this._ele_picker_options_disabled_date_max(time, max);
      },
      ...config
    }
  },
  _ele_picker_options_disabled_date(time, min, max) {
    if (time.getTime() < min) return true;
    if (time.getTime() > max) return true;
    return false;
  },
  _ele_picker_options_disabled_date_min(time, min) {
    return time.getTime() < min;
  },
  _ele_picker_options_disabled_date_max(time, max) {
    if (time.getTime() > max) return true;
    return false;
  }

}
